<?php
/**
 * Pertyler
 * Files Page
 *
 * @author	Emre Hasegeli <hasegeli@gmail.com>
 * @license	GNU General Public License v3
 */

namespace Pertyler;

require 'main.php';

if((isset($page->Table)) AND (Pertyler_Table::Select($page->Table)->FileTable()))
{
	$table = Pertyler_Table::Select($page->Table);

	$tree = $page->Tree($table);

	if($tree->Pressed() == 'all')
	{
		$rowArray = $table->SelectRowArray();

		foreach($rowArray AS $row)
		{
			$leaf = $tree->Add(new TreeLeaf($row));

			$leaf->AddParameter('ParentTable', $table->id());

			$leaf->AddParameter('Row', $row->id());

			$leaf->AddLink('row.php', 'body');
		}
	}
	else if($tree->Pressed() == 'add_okay')
	{
		try
		{
			if($table->TableType() == 'file')
				$uploadedFile = new UploadedFile('file');
			else if($table->TableType() == 'imageFile')
				$uploadedFile = new UploadedImage('file');
			else if($table->TableType() == 'documentFile')
				$uploadedFile = new UploadedDocument('file');
		}
		catch(FileException $e)
		{
			$tree->AddProperty('Upload failed');
		}

		if(isset($uploadedFile))
			if(File::Select($fileFolder . $table->uniqueName() . '/', $uploadedFile->Name(), $uploadedFile->Extension()))
				$tree->AddProperty('File exist');
			else
				try
				{
					$file = $uploadedFile->Move($fileFolder . $table->uniqueName() . '/');
				}
				catch(FileException $e)
				{
					$tree->AddProperty('Move failed');
				}

		if(isset($file))
		{
			$columnArray[1] = $uploadedFile->Name();
			$columnArray[2] = false;
			$columnArray[3] = $uploadedFile->Extension();
			$columnArray[4] = $uploadedFile->Size();

			try
			{
				$row = $table->InsertRow($columnArray);

				$tree->AddProperty('Uploaded');

				$leaf = $tree->Add(new TreeLeaf($row));

				$leaf->AddParameter('ParentTable', $table->id());

				$leaf->AddParameter('Row', $row->id());

				$leaf->AddLink('row.php', 'body');

				if($table->SelectPrivilege('approve'))
					$leaf->Colour('#006666');
			}
			catch(\Exception $e)
			{
				$tree->AddProperty('Upload failed');

				$file->Delete();
			}

			unset($columnArray);
		}
	}
	else if($tree->Pressed() == 'delete_okay')
	{
		$row = $table->SelectRow($_POST['row']);

		$columnArray = $row->SelectColumnArray();
		
		$tree->Add(new TreeLeaf($row));

		if($row->Delete())
		{
			$tree->AddProperty('Deleted.');

			$file = new File($fileFolder . $table->uniqueName() . '/', $columnArray[1]->Value(), $columnArray[3]->Value());

			$file->Delete();
		}
		else
			$tree->AddProperty('Cannot deleted.');
	}

	if($tree->Pressed() == 'add')
	{
		$tree->AddButton('add_okay');

		$tree->AddButton('cancel');

		$tree->Add(new TreeLeaf('<input type="file" name="file" />'));
	}
	else if($tree->Pressed() == 'delete')
	{
		$tree->AddButton('delete_okay');

		$tree->AddButton('cancel');

		$html = '
<select name="row" >';

		foreach($table->SelectRowArray() AS $row)
			$html .= '
	<option value="' . $row->id() . '" >
		' . $row . '
	</option>';

		$html .= '
</select>';

		$leaf = $tree->Add(new TreeLeaf($html));
	}
	else
	{
		$tree->AddButton('all');

		$tree->AddButton('add');

		$tree->AddButton('delete');
	}

	echo $tree;
}